package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.controller.DataTagController;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.DataPermQuery;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.DataTagService;
import com.denlaku.longan.util.SnowflakeUtils;
import com.denlaku.longan.vo.DataTag;
import com.denlaku.longan.vo.PageBy;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tianx
 */
@RestController
public class DataTagControllerImpl implements DataTagController {
    @Autowired
    private DataTagService dataTagService;

    @Override
    public Return<Void> add(DataTag dataTag) {
        dataTag.setId(SnowflakeUtils.nextId());
        dataTagService.add(dataTag);
        return Resp.success();
    }

    @Override
    public Return<Void> update(DataTag dataTag) {
        dataTagService.update(dataTag);
        return Resp.success();
    }

    @Override
    public Return<DataTag> get(Query query) {
        DataTag dataTag = dataTagService.get(query);
        return Resp.success(dataTag);
    }

    @VarBy
    @Override
    public Return<List<DataTag>> pageList(DataPermQuery query, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<DataTag> list = dataTagService.list(query);
        return Resp.success(list);
    }

    @Override
    public Return<List<DataTag>> search(DataPermQuery query) {
        List<DataTag> list = dataTagService.search(query);
        return Resp.success(list);
    }

    @Override
    public Return<Void> delete(DataPermQuery query) {
        if (query.getId() != null) {
            dataTagService.delete(query);
        }
        return Resp.success();
    }
}
